package de.hsos.bose.skillshare.model.core.combobox;

import java.util.List;
import java.util.Vector;

import javax.swing.AbstractListModel;
import javax.swing.ComboBoxModel;

import de.hsos.bose.skillshare.model.core.DataAccess;
import de.hsos.bose.skillshare.model.objects.SkillRating;

public class RatingComboBoxModel extends AbstractListModel implements
		ComboBoxModel {

	private static final long serialVersionUID = 1L;
	
	protected Vector<SkillRating> dataVector;
	protected SkillRating selectedObject = null;

	public RatingComboBoxModel() {
		this.dataVector = new Vector<SkillRating>();
		DataAccess da = new DataAccess();
		List<SkillRating> ratingList = da.getRolls();

		for (SkillRating rating : ratingList) {
			dataVector.add(rating);
			fireIntervalAdded(this, dataVector.size(), dataVector.size());
		}
	}

	public void setSelectedItem(Object item) {
		System.out.println("item: " + item);
		if (item instanceof SkillRating) {
			setSelectedItem(((SkillRating) item).getId());
		} else if (item instanceof String) {
			int index = -1;
			for (int i = 0; i < this.dataVector.size(); i++) {
				if (((SkillRating) this.dataVector.elementAt(i))
						.getDescription() == (String) item) {
					index = i;
					selectedObject = this.dataVector.elementAt(i);
					break;
				}
			}
			if (index < 0) {
				selectedObject = null;
			}
		} else {
			int index = -1;
			for (int i = 0; i < this.dataVector.size(); i++) {
				if (((SkillRating) this.dataVector.elementAt(i)).getId() == (Integer) item) {
					index = i;
					selectedObject = this.dataVector.elementAt(i);
					break;
				}
			}
			if (index < 0) {
				selectedObject = null;
			}
		}
		fireContentsChanged(this, -1, -1);
	}

	public Object getSelectedItem() {
		if (selectedObject == null) {
			return null;
		}
		return selectedObject.getDescription();
	}
	
	public SkillRating getCurrentItem() {
		return selectedObject;
	}

	public Object getElementAt(int i) {
		if (i <= getSize()) {
			return dataVector.elementAt(i).getDescription();
		}
		return null;
	}

	public int getSize() {
		return dataVector.size();
	}
}
